﻿var preNum="0";
var currentNum="0";
var Operation=0;
function Digit(ch) {
    if ((eval(currentNum) == 0) && (currentNum.indexOf(".") == -1))
        currentNum = ch;
    else
        currentNum = currentNum + ch;
    document.getElementById('text').value = currentNum;
}
function Equals() {
    // for when the '=' button is pressed.
    if (Operation == 1) currentNum = eval(preNum) + eval(currentNum);
    if (Operation == 2) currentNum = eval(preNum) - eval(currentNum);
    if (Operation == 3) currentNum = eval(preNum) * eval(currentNum);
    if (Operation == 4) currentNum = eval(preNum) / eval(currentNum);
    Operation = 0;
    preNum = "0";
    document.getElementById('text').value = currentNum;
}
function Op(ch) {
    // for when one of the operator buttons '/', 'x', '-', '+' is pressed.
    if (ch == '+') Operation = 1;
    if (ch == '-') Operation = 2;
    if (ch == 'x') Operation = 3;
    if (ch == '/') Operation = 4;
    preNum = currentNum;
    
    document.getElementById('text').value = currentNum;
    currentNum = "";
}

function Clear() {
    // for when the clear button 'C' is pressed and also for when the page loads
    currentNum = "0";
    Operation = "0";
    preNum = "0";
    document.getElementById('text').value = currentNum;
}
function Dot() {
    if (currentNum.length == 0) 
            currentNum = "0.";
    else if (currentNum.indexOf(".") == -1)
        currentNum = currentNum + ".";
    document.getElementById('text').value = currentNum;
}